<?php
/**
 * User: hjd-yao
 * Date: 2019/3/25
 * Time: 15:39
 */

namespace DC\UserCenter\Models\Traits;


use DC\UserCenter\Models\UserBaseModel;

trait ReferrerUser
{
    public function getDefaultReferrer($id = 0)
    {
        if(!$id){
            return self::value('user_number') ?? false;
        }

        return self::where($this->primaryKey,$id)->value('user_number');
    }
    public function checkReferrer($referrer,$field = 'user_number')
    {
        if(!self::first()){
            return ['referrer_id'=>0,'referrer_ids'=>'','tdeep'=>0];
        }

        $info = self::where($field,$referrer)->first();
        if(!$info||$info->status <0){
            $this->errorMsg = '推荐人不存在或已被冻结';
            return false;
        }

//        if($info->status == 0 && $info['is_active'] == 0){
        if($info->status == 0 ){
            $this->errorMsg = '未激活账号不能推荐注册!!!';
            return false;
        }
//        if($info->is_bd!=1){
//            $this->errorMsg = '该推荐人未报单激活!!';
//            return false;
//        }

        $referrer_ids = $info->referrer_ids?$info->referrer_ids.$info->member_id.',':','.$info->member_id.',';
        return ['referrer_id'=>$info->member_id,'referrer_ids'=>$referrer_ids,'tdeep'=>$info->tdeep+1];
    }
    //接点人
    public function getDefaultJiedian($id = 0)
    {
        if(!$id){
            return self::value('user_number') ?? false;
        }

        return self::where($this->primaryKey,$id)->value('user_number');
    }
    public function checkJiedian($jiedian,$field = 'user_number')
    {
        if(!self::first()){
            return ['jiedian_id'=>0,'jiedian_ids'=>'','jdeep'=>0];
        }

        $info = self::where($field,$jiedian)->first();
        if(!$info||$info->status <0){
            $this->errorMsg = '节点人不存在或已被冻结';
            return false;
        }

//        if($info->status == 0 && $info['is_active'] == 0){
        if($info->status == 0 ){
            $this->errorMsg = '未激活账号不能节点注册!!!';
            return false;
        }
        //其他限制
        if($info->num_zj>=3){
            //只能放两个人
            $this->errorMsg = '该节点已排满!!!';
            return false;
        }
        $jiedian_ids = $info->jiedian_ids?$info->jiedian_ids.$info->member_id.',':','.$info->member_id.',';

        return ['jiedian_id'=>$info->member_id,'jiedian_ids'=>$jiedian_ids,'jdeep'=>$info->jdeep+1];
    }
//    public function checkJiedianBytj($tuid)
//    {
//        if(!self::first()){
//            return [
//                'jiedian_id'=>0,
//                'jiedian_ids'=>'',
//                'jdeep'=>0,
//                'leftright'=>1
//            ];
//        }
//
//        $tjinfo = self::where('member_id',$tuid)->first();
//        if($tjinfo->num_tj>=2){//放在自己的右区最左端
//            $rightinfo=self::where('jiedian_id',$tuid)->where('leftright',2)->first();
//            if($rightinfo){
//                $firstinfo=$rightinfo;
//            }else{
//                //节点人是自己,且放右区
//                $jiedian_ids = $tjinfo->jiedian_ids?$tjinfo->jiedian_ids.$tjinfo->member_id.',':','.$tjinfo->member_id.',';
//                return [
//                    'jiedian_id'=>$tjinfo->member_id,
//                    'jiedian_ids'=>$jiedian_ids,
//                    'jdeep'=>$tjinfo->jdeep+1,
//                    'leftright'=>2
//                ];
//            }
//        }else{//放在自己的左区最左端
//            $firstinfo=$tjinfo;
//        }
//
//        $endinfo=UserBaseModel::where()
//            ->whereRaw("FIND_IN_SET(?,`jiedian_ids`)",[$firstinfo->member_id])
//            ->orderBy('jdeep','DESC')->first();
//
//
//        $jiedian_ids = $endinfo->jiedian_ids?$endinfo->jiedian_ids.$endinfo->member_id.',':','.$endinfo->member_id.',';
//        return ['jiedian_id'=>$endinfo->member_id,'jiedian_ids'=>$jiedian_ids,'jdeep'=>$endinfo->jdeep+1,'leftright'=>1];
//    }

    //检测报单人
    public function checkBd($referrer,$field = 'user_number')
    {
        if(!self::first()){
            return ['bd_uid'=>0];
        }

        $info = self::where($field,$referrer)->first();
        if(!$info||$info->status <0){
            $this->errorMsg = '报单人不存在或已被冻结';
            return false;
        }

        if($info->status == 0 && $info['is_active'] == 0){
            $this->errorMsg = '未激活账号不能报单注册!!!';
            return false;
        }
        if($info->is_bill == 0 ){
            $this->errorMsg = '报单人不是报单中心!!!';
            return false;
        }

        return ['bd_uid'=>$info->member_id];
    }

    public function checkReferrerByCode($code)
    {
        $info = self::where('referrer_code',$code)->first();
        if(!$info||$info->status <0){
            $this->errorMsg = '推荐人不存在或已被冻结';
            return false;
        }

        if($info->status=0 && $info['is_active'] == 0){
            $this->errorMsg = '未激活账号不能推荐注册!!！';
            return false;
        }

        $referrer_ids = $info->referrer_ids?$info->referrer_ids.$info->member_id.',':','.$info->member_id.',';
        return ['referrer_id'=>$info->member_id,'referrer_ids'=>$referrer_ids,'tdeep'=>$info->tdeep+1];
    }
}